package inet.app.controller.database;

import inet.app.model.object.KqGiai_Item;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class DaoKQGiaiXS extends DaoBase {

	public DaoKQGiaiXS(Context context) {
		super(context);
	}

	private ContentValues createContentValueFromRow(KqGiai_Item row) {

		ContentValues value = new ContentValues();
		value.put(DBConfig.COL_KQGIAI_XS_CODE, row.getCode());
		value.put(DBConfig.COL_KQGIAI_XS_NAME, row.getName());
		value.put(DBConfig.COL_KQGIAI_XS_DATE, row.getDate());
		value.put(DBConfig.COL_KQGIAI_XS_REGIONID, row.getRegionId());
		value.put(DBConfig.COL_KQGIAI_XS_GIAIDB, row.getGiaidb());
		value.put(DBConfig.COL_KQGIAI_XS_GIAI1, row.getGiai1());
		value.put(DBConfig.COL_KQGIAI_XS_GIAI2, row.getGiai2());
		value.put(DBConfig.COL_KQGIAI_XS_GIAI3, row.getGiai3());
		value.put(DBConfig.COL_KQGIAI_XS_GIAI4, row.getGiai4());
		value.put(DBConfig.COL_KQGIAI_XS_GIAI5, row.getGiai5());
		value.put(DBConfig.COL_KQGIAI_XS_GIAI6, row.getGiai6());
		value.put(DBConfig.COL_KQGIAI_XS_GIAI7, row.getGiai7());
		value.put(DBConfig.COL_KQGIAI_XS_GIAI8, row.getGiai8());
		return value;
	}

	private KqGiai_Item createRowFromCursor(Cursor cursor) {

		KqGiai_Item row = new KqGiai_Item();
		row.setId(cursor.getInt(cursor.getColumnIndex(DBConfig.COL_ID)));
		row.setCode(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_KQGIAI_XS_CODE)));
		row.setName(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_KQGIAI_XS_NAME)));
		row.setDate(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_KQGIAI_XS_DATE)));
		row.setRegionId(cursor.getInt(cursor
				.getColumnIndex(DBConfig.COL_KQGIAI_XS_REGIONID)));
		row.setGiaidb(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_KQGIAI_XS_GIAIDB)));
		row.setGiai1(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_KQGIAI_XS_GIAI1)));
		row.setGiai2(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_KQGIAI_XS_GIAI2)));
		row.setGiai3(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_KQGIAI_XS_GIAI3)));
		row.setGiai4(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_KQGIAI_XS_GIAI4)));
		row.setGiai5(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_KQGIAI_XS_GIAI5)));
		row.setGiai6(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_KQGIAI_XS_GIAI6)));
		row.setGiai7(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_KQGIAI_XS_GIAI7)));
		row.setGiai8(cursor.getString(cursor
				.getColumnIndex(DBConfig.COL_KQGIAI_XS_GIAI8)));
		return row;
	}

	public long insertRow(KqGiai_Item row) {
		if (!dbConnection.openSession())
			return -1;
		ContentValues values = createContentValueFromRow(row);
		long insertId = dbConnection.insert(DBConfig.TABLE_KQGIAI_XS, null,
				values);
		dbConnection.closeSession();
		return insertId;
	}

	public int deleteRow(int id) {
		int res = -1;
		if (!dbConnection.openSession())
			return res;
		Cursor cursor = dbConnection.query(DBConfig.TABLE_KQGIAI_XS, null,
				DBConfig.COL_ID + "=?", new String[] { String.valueOf(id) },
				null, null, null);
		if (cursor != null) {
			int rows = cursor.getCount();
			if (rows > 0) {
				/** row is existed, then delete row */
				res = dbConnection.delete(DBConfig.TABLE_KQGIAI_XS,
						DBConfig.COL_ID + "=?",
						new String[] { String.valueOf(id) });
			} else {
				/** row is not existed */
				res = 0;
			}
		}
		cursor.close();
		dbConnection.closeSession();
		return res;
	}

	public int updateRow(KqGiai_Item row) {
		int res = -1;
		KqGiai_Item old = selectRow(row.getId());
		if (old == null)
			return res;
		Cursor cursor = dbConnection.query(DBConfig.TABLE_KQGIAI_XS, null,
				DBConfig.COL_ID + "=?",
				new String[] { String.valueOf(row.getId()) }, null, null, null);
		if (cursor != null) {
			int rows = cursor.getCount();
			if (rows > 0) {
				/** row is existed, then update row */
				ContentValues values = createContentValueFromRow(row);
				res = dbConnection.update(DBConfig.TABLE_KQGIAI_XS, values,
						DBConfig.COL_ID + "=?",
						new String[] { row.getId() + "" });
			} else {
				/** row is not existed */
				res = -1;
			}
		}
		cursor.close();
		dbConnection.closeSession();
		return res;
	}

	/**
	 * find row by id
	 * 
	 * @param fileId
	 * @return
	 */
	public KqGiai_Item selectRow(int id) {
		KqGiai_Item row = null;
		try {
			Cursor cursor = null;
			if (!dbConnection.openSession())
				return row;
			cursor = dbConnection.query(DBConfig.TABLE_KQGIAI_XS, null,
					DBConfig.COL_ID + "=?",
					new String[] { String.valueOf(id) }, null, null, null);
			if (cursor != null) {
				int rows = cursor.getCount();
				if (rows > 0) {
					/** row is existed, then get row info */
					cursor.moveToFirst();
					row = createRowFromCursor(cursor);
				}
			}
			cursor.close();
			dbConnection.closeSession();
		} catch (Exception ex) {
			dbConnection.closeSession();
		}
		return row;
	}
	
	public ArrayList<KqGiai_Item> selectRowByRegion(int regionId) {
		ArrayList<KqGiai_Item> rows = new ArrayList<KqGiai_Item>();
		try {
			Cursor cursor = null;
			if (!dbConnection.openSession())
				return rows;
			cursor = dbConnection.query(DBConfig.TABLE_KQGIAI_XS, null,
					DBConfig.COL_KQGIAI_XS_REGIONID + "=?",
					new String[] { String.valueOf(regionId) }, null, null, null);
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					do {
						KqGiai_Item row = createRowFromCursor(cursor);
						rows.add(row);

					} while (cursor.moveToNext());
				}
			}
			cursor.close();
			dbConnection.closeSession();
		} catch (Exception ex) {
			dbConnection.closeSession();
		}
		return rows;
	}
	
	public long insertCollection(List<KqGiai_Item> rows) {
		if(rows == null) {
			return -1;
		}
		List<ContentValues> values = new ArrayList<ContentValues>();
		for (KqGiai_Item row : rows) {
			ContentValues value = createContentValueFromRow(row);
			values.add(value);
		}
		return insertCollection(DBConfig.TABLE_KQGIAI_XS, values);
	}

	public int deleteAll() {
		return deleteAll(DBConfig.TABLE_KQGIAI_XS);
	}

}
